
var app_family = angular.module("myApp",['ngRoute']);
/**
 * 接口调用封装
 */
app_family.factory('httpRequest', function($http, $timeout, $q) {
	var dataApi = {
		getData : function () {
			// 默认参数
		    var _httpDefaultOpts = {
		        method: 'POST', // GET/DELETE/HEAD/JSONP/POST/PUT
		        url: '',
		        params: {}, // GET方式下 拼接在url的参数
		        data: {}, // POST方式下 参数
		        cache: false, // boolean or Cache object
		        limit: true, //是否节流
		        timeout : "httpTimeout", // 节流变量名
		        timeoutTime : 100,
		        isErrMsg: false,// 错误提示
		        isErrMsgFn : null,// 错误提示函数
		        before: function () {
		        	// 加遮罩
		        	/*var appElement = document.querySelector('[ng-controller=controller_familyapp]');
		        	var loadingScope = angular.element(appElement).scope();
		        	loadingScope.loading = true;
		            loadingScope.$apply();*/
		        }, // ajax 执行开始 执行函数
		        end: function () {
		        	// 去掉遮罩
		        	/*var appElement = document.querySelector('[ng-controller=controller_familyapp]');
		        	var loadingScope = angular.element(appElement).scope();
		        	loadingScope.loading = false;
		            loadingScope.$apply();*/
		        }, // ajax 执行结束 执行函数
		        error: function () {}, // ajax 执行失败 执行函数
		        success: function (data) {}, // ajax 执行成功 执行函数
		        checkCodeError : function  (code, errMsg, data) {} // ajax 校验code失败 执行函数
		    };
		    
		    var _httpTimeoutArray = {"httpTimeout" : null}; //ajax节流使用的定时器 集合

		    var _isErrMsgFn = function (opts) {
		        if (angular.isFunction(opts.isErrMsgFn)) {
		            opts.isErrMsgFn();
		        } else {
		            layerPop.dialog.msgShow('抱歉！因为操作不能够及时响应，请稍后在试...');
		        }
		    };

		    // http请求之前执行函数
		    var _httpBefore = function (opts) {
		        if (angular.isFunction(opts.before)) {
		            opts.before();
		        }
		    };

		    // http请求之后执行函数
		    var _httpEnd = function (opts) {
		        if (angular.isFunction(opts.end)) {
		            opts.end();
		        }
		        if(opts.limit){
		            $timeout.cancel(_httpTimeoutArray[opts.timeout]);
		        }
		    };

		    // http 请求执行过程封装    deferred ：http 链式请求延迟对象
		    var _httpMin = function (opts, deferred) {
		        _httpBefore(opts);
		        $http({
		            method: opts.method,
		            url: opts.url,
		            params: opts.params,
		            data: opts.data
		        }).success(function(data, header, config, status){ //响应成功
		            // 请求成功回调函数
		            if(opts.success) {
		                opts.success(data,header,config,status);
		            }
		            if (deferred) {
		                deferred.resolve(data,header,config,status);  //任务被成功执行
		            }
		            _httpEnd(opts);
		        }).error(function(data,header,config,status){ //处理响应失败
		        	if (opts.error) {
		        		opts.error(data, header, config, status);
		        	} else {
		        		$.dialog.showToast({msgTxt : '出错啦！(状态码:' + status + ')'});
		        	}

		            if (deferred) {
		                deferred.reject(data,header,config,status); //任务未被成功执行
		            }

		            _httpEnd(opts);
		        });
		    };

		    // http请求，内含节流等控制
		    var _http = function (opts, deferred) {
		        opts = $.extend({}, _httpDefaultOpts, opts);
		        var result;
		        if (opts.limit) {
		            $timeout.cancel(_httpTimeoutArray[opts.timeout]);
		            _httpTimeoutArray[opts.timeout] = $timeout(function () {
		                result = _httpMin(opts, deferred);
		            }, opts.timeoutTime);
		        } else {
		            result = _httpMin(opts, deferred);
		        }
		    };

		    // http 链式请求
		    var _linkHttpMin = function (opts, deferred) {
		        _http(opts, deferred);
		    };

		    return {
		        /**
		         * http请求
		         * @param opts
		         */
		        http: function (opts) {
		            _http(opts);
		        },
		        /**
		         * http链式请求
		         * @param opts
		         * @param deferred
		         * @returns {deferred.promise|{then, catch, finally}}
		         */
		        linkHttp : function (opts, deferred) {
		            deferred = deferred || $q.defer();
		            _linkHttpMin(opts, deferred);
		            return deferred.promise;
		        }
		    };
		},
		// 登录
		login: function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST', // 也可不写，默认是POST方式
				data : params,
				url : basepath + "loginController/login.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 发送验证码
		sendCode: function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "loginController/sendCode.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 获取省市
		getProvinceAndCity: function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "loginController/getProvinceAndCity.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据城市Code，获取区县
		getCountyByCity : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "loginController/getCountyByCity.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 注册
		register : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "loginController/register.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 登录后，首页初始化
		initIndex : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "loginController/initIndex.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据服务机构Code，查询服务机构信息
		getOrganizationInfo : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "loginController/getOrganizationInfo.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 绑定
		bindOlder : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "interactionController/bindOlder.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 获取家属绑定的所有
		getAllOlder : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "interactionController/getAllOlder.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 获取日常生活问题和答案
		getDailyQuestion : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "assessController/getDailyQuestion.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 获取精神状态问题和答案
		getSpiritQuestion : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "assessController/getSpiritQuestion.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 获取感知觉与沟通问题和答案
		getFeelQuestion : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "assessController/getFeelQuestion.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 获取社会参与问题和答案
		getSocialQuestion : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "assessController/getSocialQuestion.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 获取能力等级问题和答案
		getAbilityQuestion : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "assessController/getAbilityQuestion.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 获取能力等级问题和答案
		saveOlderAssess : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "assessController/saveOlderAssess.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据家属手机号，查询所有关联的评估结果
		getOlderAssessRecord : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "assessController/getOlderAssessRecord.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据评估编号，查询评估建议
		getAssessSuggestion : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "assessController/getAssessSuggestion.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据家属手机号，查询家属个人数据
		getMineInfo : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/getMineInfo.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 修改家属个人信息
		updateMineInfo : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/updateMineInfo.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 修改家属个人头像
		updateMineIcon : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/updateMineIcon.htm",
				success : callbackS,
				error : callbackE
			});
		},
		//根据账号、日期，获取所有服务订单
		getAllOrder : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/getAllOrder.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号、日期，获取生活日历   account 账号 date 查询日期(yyyy-mm-dd)
		getLiveDayByOlder : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/getLiveDayByOlder.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号，查询个人信息
		getOlderInfo : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "interactionController/getOlderInfo.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 修改个人信息
		updateOlderInfo : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "interactionController/updateOlderInfo.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 修改头像
		updateOlderIcon : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "interactionController/updateOlderIcon.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 解除绑定
		delBindOlder : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "interactionController/delBindOlder.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 发送信息
		sendInfo : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "interactionController/sendInfo.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 查询代付订单数据
		getOrderForPaying : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "orderController/getOrderForPaying.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 查询家属帮预订的服务单数据
		getOrderByFamily : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "orderController/getOrderByFamily.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 家属取消帮预订的服务订单
		cancelOrder : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "orderController/cancelOrder.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据经纬度，查询当前范围内的所有服务项目
		getAllService : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "orderController/getAllService.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 查询服务商及服务价格
		getSupplierAndPrice : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "orderController/getSupplierAndPrice.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 家属下单
		saveOrder : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "orderController/saveOrder.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据订单，生成支付宝支付链接信息
		getPayLink : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "payController/getPayLink.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 家属支付成功，改变订单状态为已支付
		updateOrderStatusForPay : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "orderController/updateOrderStatusForPay.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 通过订单编号，查询订单信息
		getOrderById : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "orderController/getOrderById.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号、日期，获取体检健康档案数据
		getTestInfoByOlder : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/getTestInfoByOlder.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号、日期类型、护理类型查询图形数据
		searchLocus : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/searchLocus.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号、日期类型，查询订餐订单数据
		getCookOrderByOlder : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/getCookOrderByOlder.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号，查询护理计划
		selectCustomerPlan : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/selectCustomerPlan.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号、起止时间，查询护理实绩
		getAllDayService : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/getAllDayService.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号、日期、生命体征码，查询生命体征
		getToDayService : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/getToDayService.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号、起止时间，查询每日消费明细
		getOlderBills : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/getOlderBills.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 根据账号，查询账户信息
		findOlderAccount : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "mineController/findOlderAccount.htm",
				success : callbackS,
				error : callbackE
			});
		},
		// 获取最近两天的聊天记录
		getChatRecord : function (params, callbackS, callbackE) {
			this.getData().http({
				method: 'POST',
				data : params,
				url : basepath + "interactionController/getChatRecord.htm",
				success : callbackS,
				error : callbackE
			});
		}

	};
	
	return dataApi;
});